import Vue from 'vue';
import Router from 'vue-router';
import { MessageBox } from 'element-ui';
import { getStore, setStore } from '../utils/storage';

const Layout = () => import('@/views/public/LayoutContainer.vue'); // 公用模板
const Login = () => import('@/views/public/Login.vue'); // 登录
const Home = () => import('@/views/public/Home.vue'); // 首页
const UserList = () => import('@/views//user/UserList.vue'); // 个人列表
const UserGuide = () => import('@/views/user/UserGuide.vue'); // 用户指南
const OnlineInquiry = () => import('@/views/inquiry/OnlineInquiry.vue'); // 网络问诊
const VideoInquiryRoom = () => import('@/views/videoroom/VideoInquiryRoom.vue'); // 聊天室
const MyPrescription = () => import('@/views/prescription/MyPrescription.vue'); // 我的处方
const MyInquiry = () => import('@/views/userInquiry/MyInquiry.vue'); // 我的处方
const PageTest = () => import('@/views/public/PageTest.vue'); // 测试页面

Vue.use(Router);

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

const VueRouter = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login,
    },
    {
      path: '/',
      redirect: '/index',
      name: 'Layout',
      component: Layout,
      // 嵌套路由
      children: [
        {
          path: '/home',
          name: 'home',
          component: Home,
        },
        {
          path: '/user',
          name: '个人列表',
          component: UserList,
        },
        {
          path: '/guide',
          name: '用户指南',
          component: UserGuide,
        },
      ],
    },
    {
      path: '/',
      redirect: '/index',
      name: 'Inquiry',
      component: Layout,
      // 嵌套路由
      children: [
        {
          path: '/inquiry',
          name: '网络问诊',
          component: OnlineInquiry,
        },
        {
          path: '/video',
          name: '聊天室',
          component: VideoInquiryRoom,
        },
        {
          path: '/prescription',
          name: '自主开方',
          component: () => import('@/views/videoroom/SubstituteRoom'),
        },
      ],
    },

    {
      path: '/',
      redirect: '/index',
      name: 'Prescription',
      component: Layout,
      // 嵌套路由
      children: [
        {
          path: '/myprescription',
          name: '我的处方',
          component: MyPrescription,
        },
      ],
    },
    {
      path: '/',
      redirect: '/index',
      name: 'UserInquiry',
      component: Layout,
      // 嵌套路由
      children: [
        {
          path: '/myInquiry',
          name: '我的问诊',
          component: MyInquiry,
        },
      ],
    },
    {
      path: '/interface',
      name: 'interface',
      component: () => import('@/views/inquiry/doctorinquiry/interface-page'),
    },
    {
      path: '/',
      redirect: '/index',
      name: 'RemoteConsult',
      component: Layout,
      // 嵌套路由
      children: [
        {
          path: '/remoteConsult',
          name: 'remoteConsult',
          redirect: '/remoteConsult/myConsult',
          component: () => import('@/views/remoteConsult'),
          children: [
            {
              path: '/remoteConsult/myConsult',
              name: 'myConsult',
              component: () => import('@/views/remoteConsult/myConsult/index.vue'),
            },
            {
              path: '/remoteConsult/apply',
              name: 'myConsultApply',
              component: () => import('@/views/remoteConsult/myConsult/apply.vue'),
            },
            {
              path: '/remoteConsult/detail',
              name: 'myConsultDetail',
              component: () => import('@/views/remoteConsult/myConsult/detail.vue'),
            },
          ],
        },
        {
          path: '/startConsult',
          name: '会诊室',
          component: () =>
            import('@/views/remoteConsult/myConsult/startConsult'),
        },
      ],
    },
    {
      path: '/*',
      redirect: '/home',
    },
  ],
});

const routerWhiteList = ['/login', '/home'];
VueRouter.beforeEach((to, from, next) => {
  if (!getStore('accessToken') && to.path != '/login' && to.path != '/home') {
    // 判断未登录，且前往的页面不是白名单路由
    sessionStorage.clear();
    next({
      path: '/login',
    });
  } else if (getStore('accessToken') && to.name == 'login') {
    // 判断已登录，且前往的是登录页
    next({
      path: '/home',
    });
  } else {
    next();
  }
});

/* VueRouter.onError((error) => {
    const pattern = /Loading chunk (\d)+ failed/g;
    const isChunkLoadFailed = (error.message).match(pattern);
    if (isChunkLoadFailed) {
        MessageBox.alert('系统已更新，请刷新页面！', '', {
            confirmButtonText: '刷 新',
            type: 'warning',
            showClose: false,
            closeOnClickModal: false,
            closeOnPressEscape: false,
            callback: action => {
                window.location.reload()
            }
        })
    }
}); */

export default VueRouter;
